package org.desktop.web.client.role.form;

import org.desktop.web.client.gui.models.APagingGrid;
import org.desktop.web.client.gui.models.AbstractWindowForm;
import org.desktop.web.client.images.Resources;
import org.desktop.web.client.role.RoleService;
import org.desktop.web.client.role.form.handler.CodRoleHandler;
import org.desktop.web.client.role.form.validator.CodRoleValidator;
import org.desktop.web.client.util.Util;
import org.desktop.web.shared.Response;
import org.desktop.web.shared.domain.entities.Role;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.button.ButtonBar;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.TextField;

public class RoleFormWindow extends AbstractWindowForm<Role>{
	
	

	private APagingGrid<Role, ?, ?> grid;

	public RoleFormWindow(
			org.desktop.web.client.gui.models.AbstractWindowForm.OperationType operationType,APagingGrid<Role, ?, ?> grid) {
		super(operationType);
		this.grid=grid;
	}

	private TextField txtCodRole;
	private TextField txtDescRole;

	@Override
	public String id() {
		// TODO Auto-generated method stub
		return "ROLE_FORM";
	}

	@Override
	public IsWidget createLayout() {	
		this.setWidth(400);
		this.setHeight(400);
		
		VerticalLayoutContainer vlcMain = new VerticalLayoutContainer();
		
		this.txtCodRole = new TextField();
		
		this.txtCodRole.setAllowBlank(false);
		vlcMain.add(new FieldLabel(this.txtCodRole, "Codice ruolo"),
				new VerticalLayoutData(1, -1, new Margins(5, 10, 5, 10)));
		
		CodRoleValidator codRoleValidator = new CodRoleValidator();
		CodRoleHandler codRoleandler = new CodRoleHandler(this.txtCodRole,codRoleValidator); 
		
		this.txtCodRole.addValueChangeHandler(codRoleandler);
		this.txtCodRole.addKeyUpHandler(codRoleandler);
		this.txtDescRole = new TextField();
		this.txtDescRole.setAllowBlank(false);
		vlcMain.add(new FieldLabel(this.txtDescRole, "Descrizione ruolo"),
				new VerticalLayoutData(1, -1, new Margins(5, 10, 5, 10)));
		
		

		
		return vlcMain;
		
	}


	@Override
	public SelectHandler getInsertHandler() {
		
		SelectHandler handler = new SelectHandler() {
			
			@Override
			public void onSelect(SelectEvent event) {
				// TODO Auto-generated method stub
				if(!txtCodRole.isValid() || !txtDescRole.isValid()){
					Util.showAlert("Attenzione!!!", "I dati inseriti contengono errori!", MessageBox.ICONS.error());
					return;
				}
				Role role = new Role();
				role.setCodRole(txtCodRole.getValue());
				role.setDescRole(txtDescRole.getValue());
				RoleService.ServiceFactory.createService().saveRole(Util.getHeaders(),role,new AsyncCallback<Response>() {

					@Override
					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(Response response) {
						if(response.getResponseCode()==201){
							Util.showAlert("", "Salvataggio avvenuto con successo!", MessageBox.ICONS.info());
							hide();
							grid.load();
							
							return;
						}
					}
				});
			}
		};
		
		return handler;
	}

	@Override
	public SelectHandler getUpdateHandler() {
		SelectHandler handler = new SelectHandler() {
			
			@Override
			public void onSelect(SelectEvent event) {
				// TODO Auto-generated method stub
				
			}
		};
		
		return handler;
	}

}
